package org.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.domain.po.ErpMenu;
import org.example.service.ErpMenuService;
import org.example.mapper.ErpMenuMapper;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author DELL
* @description 针对表【erp_menu(功能模块表)】的数据库操作Service实现
* @createDate 2024-10-11 10:34:47
*/
@Service
public class ErpMenuServiceImpl extends ServiceImpl<ErpMenuMapper, ErpMenu>
    implements ErpMenuService{
    
    /**
     * 租户使用的树形菜单
     */
    @Override
    public List<ErpMenu> findMenusAdminTree(Integer parentId, String cname) {
        return baseMapper.findMenusAdminTree(parentId, cname);
    }
    
    /**
     * 根据用户ID查询菜单
     * @param userId 用户ID
     * @param parentId 菜单ID
     */
    @Override
    public List<ErpMenu> findMenuByUserIdTree(Long userId, Long parentId) {
        return baseMapper.findMenuByUserIdTree(userId, parentId);
    }
    
    /**
     * 管理员查询菜单功能(无树形)
     */
    @Override
    public List<ErpMenu> findMenusAdmin() {
        return lambdaQuery().list();
    }
    
    /**
     * 根据用户ID查询菜单功能
     * @param userId 用户ID
     */
    @Override
    public List<ErpMenu> findMenusByUserId(Long userId) {
        List<ErpMenu> menusByUserId = baseMapper.findMenusByUserId(userId);
        // 根据菜单id排序
        menusByUserId = menusByUserId.stream().sorted(Comparator.comparing(ErpMenu::getId)).collect(Collectors.toList());
        return menusByUserId;
    }
    
    /**
     * 根据用户ID查询菜单（不包括顶级菜单）
     * @param userId 用户ID
     */
    @Override
    public List<ErpMenu> getMenusByUserId(Long userId) {
        return baseMapper.getMenusByUserId(userId);
    }
    
    
    @Override
    public List<ErpMenu> findMenusRoleId(Long roleId, Long tenantId) {
        return baseMapper.findMenusRoleId(roleId, tenantId);
    }
    
    @Override
    public List<ErpMenu> queryMenuByParentId(Long menuId) {
        return baseMapper.queryMenuByParentId(menuId);
    }
}




